import java.util.HashSet;
import java.util.Set;

/**
 * @author LKQ
 * @date 2022/1/21 10:44
 * @description
 */
public class Solution {
    public static void main(String[] args) {

    }

    public ListNode detectCycle(ListNode head) {
        Set<ListNode> set = new HashSet<>();
        ListNode ans = null;
        while (head != null) {
            if (!set.add(head)) {
                ans = head;
                break;
            }
            head = head.next;
        }
        return ans;
    }
}
